package com.imooc.service.impl;

import com.imooc.dto.OrderDTO;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.service.OrderMasterService;
import com.imooc.service.PayService;
import com.imooc.util.MathUtil;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**pay service implements class
 * @author liuhuan
 * @date 2018/7/21 08:51
 * @description
 */
@Service
@Slf4j
public class PayServiceImpl implements PayService {
    public static final String ORDER_NAME="微信点餐系统";

    @Autowired
    private BestPayServiceImpl bestPayService;

    @Autowired
    private OrderMasterService orderMasterService;

    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest payRequest=new PayRequest();
        payRequest.setOpenid(orderDTO.getOrderId());
        payRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        payRequest.setOrderId(orderDTO.getOrderId());
        payRequest.setOrderName(ORDER_NAME);
        payRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信支付】request={}",payRequest);
        PayResponse payResponse=bestPayService.pay(payRequest);
        log.info("【微信支付】payResponse={}",payResponse);

        return  payResponse;
    }

    @Override
    public PayResponse notify(String notifyData) {

        //a.验证签名
        //b.支付状态
       // c.支付金额
        //d.支付人（下单人==支付人）
        //a.bsdk已经做了，现在需要判断c

        PayResponse payResponse=bestPayService.asyncNotify(notifyData);
        log.info("【微信支付】异步通知，payResponse={}",payResponse);
        //修改订单支付状态
        //查询订单
        OrderDTO orderDTO=orderMasterService.findOne(payResponse.getOrderId());
        //判断订单是否存在
        if(orderDTO==null){
            log.error("【微信支付】异步通知，订单不存在，orderId={}",payResponse.getOrderId());
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        //判断金额是否一致（0.1=0.10）
        if(!MathUtil.equal(orderDTO.getOrderAmount().doubleValue(),payResponse.getOrderAmount())){
            log.error("【微信支付】异步通知，金额不一致，系统金额={}，微信金额={}",orderDTO.getOrderAmount(),payResponse.getOrderAmount());
            throw new SellException(ResultEnum.WX_NOTIFY_AMOUNT);
        }
        //修改支付状态
        orderMasterService.paid(orderDTO);
        return payResponse;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest refundRequest=new RefundRequest();
        refundRequest.setOrderId(orderDTO.getOrderId());
        refundRequest.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        refundRequest.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        log.info("【微信退款】refundRequest={}",refundRequest);
        RefundResponse refundResponse=bestPayService.refund(refundRequest);
        log.info("【微信退款】refundResponse={}",refundResponse);
        return refundResponse;
    }
}
